﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Imaginecup.gameState.recycling.Sprite;
using Imaginecup.observer;
using Imaginecup.gameState.recycling.Observable;
using Imaginecup.recycling.Object.Entity;
using Imaginecup.gameState.recycling.Entity;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Imaginecup.util;
using Microsoft.Xna.Framework.Graphics;
using Imaginecup.recycling.Object.Menu;
using Imaginecup.Recycling.GameMan;
using Imaginecup.util.StaticValue;
using Imaginecup.gameState.recycling.Sprite.Factory;

namespace Imaginecup.gameState.recycling.Object.UI
{
    public struct ItemData{
        public Button_State State;
        public String Name;
    };

    public class ItemButton : IMyObserver, IUpdate, ISprite, IValueChanged
    {
        private const int MAX_VALUE = 100;
        private const int BUFF_VALUE = 75;
        private int m_CurrentItemValue = 0;
        private Button_State m_CurrentState;
        private ISprite m_ImageSprite;
        private ISprite m_PercentSprite;
        private ISprite m_InvisibleSprite;

        private IObservable m_GroupObservable;
        private ObjectInfo m_percentSpriteInfo;

        private const float TOGGLE_TIME = 1.0f;
        private float m_TotalDrawToggleTime = 0;
        
        private const float TIME = StaticValues.COOL_TIME;
        private float m_TotalGameTime = 0;

        private ItemData m_BaseData;

        public ItemButton(ObjectInfo info,String spriteName, IObservable group)
        {
            SpriteFactory factory = SpriteFactory.GetInstance();
            m_ImageSprite = (ISprite)factory.GetObject(SpriteID.IMAGE_TOGGLE_SPRITE,info, spriteName);
            m_percentSpriteInfo = new ObjectInfo(info.SourceRectangle);
            m_percentSpriteInfo.ImageRectangle = new Rectangle(m_percentSpriteInfo.FrameWidth, m_percentSpriteInfo.FrameHeight, m_percentSpriteInfo.FrameWidth, 0);
            m_PercentSprite = (ISprite)factory.GetObject(SpriteID.NORMAL_SPRITE, m_percentSpriteInfo, spriteName);
            m_InvisibleSprite = (ISprite)factory.GetObject(SpriteID.NORMAL_SPRITE, info, "InvisibleButton");

            m_GroupObservable = group;

            m_BaseData = new ItemData();
            m_BaseData.Name = spriteName;

            RenderDrawer.GetInstance().AddSprite(this);
            Updater.GetInstance().AddUpdate(this);
        }

        public void Update(IObservable ob, object obj)
        {
            if( obj is ItemData)
            {
                m_CurrentState = ((ItemData)obj).State;
                ((ImageToggleSprite)m_ImageSprite).SetButtonFlag(false);
            }
        }

        public void SetValue(int value)
        {
            ValueUpdate(value);
        }

        private void ValueUpdate(int value)
        {
            m_CurrentItemValue += value;
            if (m_CurrentItemValue > MAX_VALUE)
                m_CurrentItemValue = 100; // 여기에 게임 오버 가능할 듯
            else if (m_CurrentItemValue <= 0)
            {
                m_CurrentItemValue = 0;
                m_BaseData.State = Button_State.NONE;
                m_GroupObservable.NotifyObservers(m_BaseData);
            }
            float ratio = (float)m_percentSpriteInfo.FrameHeight * (float)(m_CurrentItemValue / 100.0f);
            m_percentSpriteInfo.SetImageRectByRatio((int)ratio);
        }
                
        public void Draw(SpriteBatch spriteBatch, GameTime gameTime)
        {
            //None상태의 버튼을 그린다
            m_ImageSprite.Draw(spriteBatch, gameTime);

            if (m_CurrentState == Button_State.PRESSED)
            {
                float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
                m_TotalDrawToggleTime += elapsed;
                if (m_TotalDrawToggleTime >= TOGGLE_TIME)
                {
                    m_TotalDrawToggleTime -= TOGGLE_TIME;
                    //return;
                }
            }
            // 아이템의 수치에 따라 레이어 추가
            if (m_CurrentItemValue > 0)
                m_PercentSprite.Draw(spriteBatch, gameTime);
            // LOCK상태에서는 투명 레이어 추가
            if (m_CurrentState == Button_State.LOCK)
                m_InvisibleSprite.Draw(spriteBatch, gameTime);
        }
        
        // 입력처리
        public void Update(GameTime gameTime)
        {
            if (m_CurrentState == Button_State.PRESSED)
            {
                //버튼이 눌러진 상태라면 분리수거를 실행하여 값을 감소
                float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
                m_TotalGameTime += elapsed;

                if (m_TotalGameTime >= TIME)
                {
                    m_TotalGameTime -= TIME;
                    ValueUpdate(-5);
                }
            }

            if (MouseInputHandler.GetInputHandler().IsOneClick())
            {
                ObjectInfo info = m_ImageSprite.GetInfo();
                MouseState mouse = MouseInputHandler.GetInputHandler().MouseState;

                if (m_CurrentState == Button_State.PRESSED)
                {
                    m_BaseData.State = Button_State.NONE;
                    m_GroupObservable.NotifyObservers(m_BaseData);
                    return;
                }

                if (info.SourceRectangle.Contains(mouse.X, mouse.Y))
                {
                    m_BaseData.State = Button_State.LOCK;
                    m_GroupObservable.NotifyObservers(m_BaseData);
                    m_CurrentState = Button_State.PRESSED;
                    ((ImageToggleSprite)m_ImageSprite).SetButtonFlag(true);
                }
            }
        }

        public ObjectInfo GetInfo()
        {
            throw new NotImplementedException();
        }
    }
}
